home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / science / ack3d.zip / ACKIFF.C < prev    next >
Text File  |  1994-01-09  |  5KB  |  189 lines

  1. /******************* ( Animation Construction Kit 3D ) ***********************/
  2. /*            Deluxe Paint file reader                 */
  3. /* CopyRight (c) 1993  Authors: Jaimi McEntire, Lary Myers             */
  4. /*****************************************************************************/
  5. //
  6. // This function will return a pointer to a buffer that holds the raw image.
  7. // just free the pointer to delete this buffer. After returning, the array
  8. // colordat will hold the adjusted palette of this pic.
  9. //
  10. // Also, this has been modified to only read in form PBM brushes. form ILBM's
  11. // (the "old" type) are not supported. use the "new" deluxe paint .lbm type
  12. // and do not choose "old".
  13.  
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <process.h>
  17. #include <bios.h>
  18. #include <fcntl.h>
  19. #include <malloc.h>
  20. #include <mem.h>
  21. #include "ack3d.h"
  22. #include "ackeng.h"
  23. #include "ackext.h"
  24. #include "iff.h"
  25.  
  26.  
  27. unsigned char far  colordat[768];  /* maximum it can be...256 colors    */
  28.  
  29. unsigned char far  cplanes[8][80]; /* setting max at 640 pixels width    */
  30.                    /* thats 8 pixels per byte per plane */
  31. unsigned char far  *pplanes= (char far *) &cplanes[0][0];  /* for a form pbm        */
  32.  
  33. unsigned char far * AckReadiff(char *picname)
  34.    {
  35.    FILE *pic;
  36.    form_chunk    fchunk;
  37.    ChunkHeader    chunk;
  38.    BitMapHeader bmhd;
  39.    long length;
  40.    char value;       // must remain signed, no matter what. ignore any warnings.
  41.    int sofar;
  42.    int width,height,planes;
  43.    int pixw;
  44.    unsigned char far *destx, *savedestx;
  45.  
  46.    if ((pic = fopen(picname,"r+b"))== NULL)
  47.     {
  48.     ErrorCode = ERR_BADPICNAME;
  49.     return(0L);
  50.     }
  51.  
  52.    fread(&fchunk,1,sizeof(form_chunk),pic); /* read in the first 12 bytes*/
  53.  
  54.    if (fchunk.type != FORM)
  55.       {
  56.       fclose(pic);
  57.       ErrorCode = ERR_INVALIDFORM;
  58.       return(0L);
  59.       }
  60.  
  61.    if (fchunk.subtype != ID_PBM)
  62.       {
  63.       printf("Error: Not form PBM!");
  64.       fclose(pic);
  65.       ErrorCode = ERR_NOPBM;
  66.       return(0L);
  67.       }
  68.    // now lets loop...Because the Chunks can be in any order!
  69.    while(1)
  70.       {
  71.       fread(&chunk,1,sizeof(ChunkHeader),pic);
  72.       ByteFlipLong(&(long)(chunk.ckSize));
  73.       if (chunk.ckSize & 1) chunk.ckSize ++;    // must be word aligned
  74.       if(chunk.ckID == ID_BMHD)
  75.       {
  76.       fread(&bmhd,1,sizeof(BitMapHeader),pic);
  77.       bmhd.w=swab(bmhd.w);            // the only things we need.
  78.       bmhd.h=swab(bmhd.h);
  79.       destx = (unsigned char far *)malloc((bmhd.w * bmhd.h)+4);
  80.       if ( !destx )
  81.         {
  82.         fclose(pic);
  83.         ErrorCode = ERR_NOMEMORY;
  84.         return(0L);
  85.         }
  86.  
  87.       savedestx = destx;
  88.  
  89.       destx[0] = bmhd.w%256;
  90.       destx[1] = bmhd.w/256;
  91.       destx[2] = bmhd.h%256;
  92.       destx[3] = bmhd.h/256;
  93.       destx += 4;
  94.       continue;
  95.       }
  96.       if(chunk.ckID == ID_CMAP)
  97.       {
  98.       int i;
  99.       unsigned char r,g;
  100.  
  101.       fread(colordat,1,chunk.ckSize,pic);
  102.       for (i=0;i<768;i++)
  103.           {
  104.            r = colordat[i];      // r,g do not stand for red and green
  105.           g = r >> 2;
  106.           colordat[i] = g;
  107.           }
  108.       continue;
  109.       }
  110.       if(chunk.ckID == ID_BODY)
  111.       {
  112.       for(height = 0; height<bmhd.h; height ++)
  113.          {
  114.          unsigned char *dest;
  115.          dest = (unsigned char *)&(pplanes[0]); /* point at first char  */
  116.          sofar = bmhd.w;                /* number of bytes = 8  */
  117.          if (sofar&1) sofar++;
  118.          while (sofar)
  119.         {
  120.         if (bmhd.compression)
  121.            {
  122.            value=fgetc(pic);      /* get the next byte      */
  123.            // if (value == 128) continue; /* NOP..just get another*/
  124.            if (value > 0)
  125.               {
  126.               int len;
  127.               len = value +1;
  128.               sofar -= len;
  129.               if(!(fread(dest,len,1,pic)))
  130.               {
  131.               fclose(pic);
  132.               ErrorCode = ERR_BADPICFILE;
  133.               free(savedestx);
  134.               return(0L);
  135.               }
  136.               dest +=len;
  137.               }
  138.            else
  139.               {
  140.               int count;
  141.               count = -value; /* get amount to dup */
  142.               count ++;
  143.               sofar -= count;
  144.               value=fgetc(pic);
  145.               while (--count >= 0) *dest++ = value;
  146.               }
  147.            }
  148.         else
  149.            {
  150.            fread(dest,sofar,1,pic); /* just throw on plane */
  151.            sofar = 0;
  152.            }
  153.         }
  154.          if (sofar < 0)
  155.         {
  156.         fclose(pic);
  157.         }
  158.          _fmemcpy(destx,pplanes,bmhd.w);
  159.          destx += bmhd.w;
  160.          }
  161.       break; /* leave if we've unpacked the BODY*/
  162.       }
  163.       fseek(pic,chunk.ckSize,SEEK_CUR);
  164.       }
  165.  
  166.    fclose(pic);
  167.    return((char far *)savedestx);
  168.    }
  169.  
  170. void ByteFlipLong(long *NUMBER)
  171.    {
  172.    // Hey, I didn;t write this function!!!
  173.    long int Y, T;
  174.    int I;
  175.  
  176.    T = *NUMBER;
  177.    Y=0;for (I=0;I<4;I++){Y = Y | (T & 0xFF);if (I<3) {Y = Y << 8;T = T >> 8;}}
  178.    *NUMBER = Y;
  179.    }
  180.  
  181. int swab(unsigned short number)
  182.    {
  183.    unsigned int xx1,xx2;
  184.    unsigned int result;
  185.  
  186.    xx1 = number <<8; xx2 = number >>8; result = xx1|xx2;
  187.    return(result);
  188.    }
  189.